import gui.IntervalView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import svm.Interval;
import svm.Session;
import svm.LuxandDataUtils;
import svm.Recognizing;
import svm.Testing;
import svm.Training;
import utils.Logger;
import au.AU;
import au.AU_01_left;
import au.AU_01_right;
import au.AU_02_left;
import au.AU_02_right;
import au.AU_12_left;
import au.AU_12_right;
import au.AU_23_left;
import au.AU_23_right;
import au.AU_26;

/**
 * 
 * ARGS luxand <parent-dir-name>
 * 
 * */ 
 
public class Main {

	static LinkedList<AU> aus = new LinkedList<AU>();
	
	public static void main(String[] args) throws Exception {

		Logger.startProcess();
		
		aus.add(new AU_12_left());
		aus.add(new AU_12_right());
		aus.add(new AU_23_left());
		aus.add(new AU_23_right());
		aus.add(new AU_26());
		aus.add(new AU_01_left());
		aus.add(new AU_01_right());
		aus.add(new AU_02_left());
		aus.add(new AU_02_right());
		
		try {
			if (args[0].equals("luxand")) { // Create Luxand data
				LuxandDataUtils.createLuxandData(args[1]);
			} else if(args[0].equals("divide")) { // Divide Luxand data to training and testing set
				LuxandDataUtils.divideLuxandData(args[1]);
			} else if(args[0].equals("train")) { // Train classifier
				 Training training = new Training(args[1], aus);
				 Testing test = new Testing(args[2], aus);
				 training.trainForAUs(aus);
				 test.testForAUs(aus);
			} else if(args[0].equals("recognize")) {
				recognitionSession();
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("No command given");
		}

//		Recognizing rec = new Recognizing("/_mate_data/dp_experiment/teap_22-3-2013", aus);
//		rec.recognize(null, null);
//		rec.save("rec_teap_full");
		
		
		//recognitionSession();
		
		Recognizing rec = (Recognizing) Recognizing.load("/_mate_data/dp_experiment/teap_22-3-2013/rec2");
		
		rec.createIntervals(20, rec.getSessions());
		
		List<Interval> intervals = new ArrayList<Interval>();
		new IntervalView(rec.getSessions().get(5).getIntervals());
		
		Logger.endProcess();
		
		System.out.println("\n\n-----\nDONE");

	}

	private static void recognitionSession() throws IOException {
	    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    String s;
	    Recognizing rec = null;
	    while ((s = in.readLine()) != null && s.length() != 0) {
	    	try {
		    	if(s.equals("quit")) break;
		    	else if(s.split(" ")[0].equals("new")) {
		    		rec = new Recognizing(s.split(" ")[1], aus);
		    	}
		    	else if(s.split(" ")[0].equals("open")) {
		    		rec = (Recognizing) Recognizing.load(s.split(" ")[1]);
		    	}
		    	else if(s.split(" ")[0].equals("save")) {
		    		if(rec != null) rec.save(s.split(" ")[1]);
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.split(" ")[0].equals("load")) {
		    		if(rec != null) rec.loadLuxandData(null);
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.split(" ")[0].equals("loadWithActivities")) {
		    		HashMap<String, String> keywords = new HashMap<String, String>();
		    		System.out.print("\t");
		    		while ((s = in.readLine()) != null && s.length() != 0) {
		    			if(s.split(" ").length > 1) keywords.put(s.split(" ")[0], s.split(" ")[1]);
		    			else System.out.println("\tWARNING: Give 2 strings separated by space!");
		    		}
		    		if(rec != null) rec.loadLuxandData(keywords);
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.split(" ")[0].equals("intervals")) {
		    		if(rec != null) {
		    			if(s.split(" ").length > 1) rec.createIntervals(Integer.parseInt(s.split(" ")[1]), null);
		    			else System.out.println("WARNING: No window length given!");
		    		}
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.split(" ")[0].equals("recognize")) {
		    		if(rec != null) {
		    			if(s.split(" ").length == 3)
		    				rec.recognize(Integer.parseInt(s.split(" ")[1]), Integer.parseInt(s.split(" ")[2]));
		    			else
		    				rec.recognize(null, null);
		    		}
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.equals("print")) {
		    		if(rec != null) for(Session ld: rec.getSessions()) System.out.println(ld.toString());
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.equals("printWithIntervals")) {
		    		if(rec != null) for(Session ld: rec.getSessions()) System.out.println(ld.toStringWithIntervals());
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.equals("printWithActivities")) {
		    		if(rec != null) for(Session ld: rec.getSessions()) System.out.println(ld.toStringWithActivities());
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.equals("printWithRows")) {
		    		if(rec != null) for(Session ld: rec.getSessions()) System.out.println(ld.toStringWithRows());
		    		else System.out.println("WARNING: No instance!");
		    	}
		    	else if(s.equals("help")) {
		    		help();
		    	} else {
		    		System.out.println("WARNING: Invalid command!");
		    	}
	    	} catch(Exception e) {
	    		e.printStackTrace();
	    	}
	    }
		
	}

	private static void help() {
		System.out.println("----- HELP CONTENT -----");
		System.out.println("Context creating commands:");
		System.out.println("- new <parent-dir>");
		System.out.println("- open <file-path>");
		System.out.println("Context operations:");
		System.out.println("- save <fine-name>");
		System.out.println("- load");
		System.out.println("- loadWithActivities");
		System.out.println("- recognize");
		System.out.println("- recognize <from-session> <to-session>");
		System.out.println("- intervals <window-length>");
		System.out.println("- print");
		System.out.println("- printWithIntervals");
		System.out.println("- printWithRows");
		System.out.println("- printWithActivities");
		
	}

}
